package com.sophimp.jumphack;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 梦幻登天梯
 */
public class MyPosFinder2 {

    // 剑鞘颜色
    public static final int R_TARGET_SWORD = 116;
    public static final int G_TARGET_SWORD = 64;
    public static final int B_TARGET_SWORD = 28;

    // 左边脚后跟颜色
    public static final int R_TARGET_LEFT = 28;
    public static final int G_TARGET_LEFT = 24;
    public static final int B_TARGET_LEFT = 26;

    /**
     * 最左边坐标
     */
    private static final int MIN_X = 0;

    /**
     * 最右边坐标
     */
    private static final int MAX_X = 1080;

    private static final int MIN_Y = 880;
    private static final int MAX_Y = 1150;

    /**
     * 中间分隔线
     */
    private static final int MIDDLE_X = 540;

    /**
     * 跳完10步后的最大值
     */
    private static final int MAX_Y_10 = 1000;
    public int[] find(BufferedImage image) {
        if (image == null) {
            return null;
        }
        int width = image.getWidth();
        int height = image.getHeight();

        int[] ret = {0, 0};
        int maxX = Integer.MIN_VALUE;
        int minX = Integer.MAX_VALUE;
        int maxY = -1;
        int minY = -1;
        // 找脚后跟, 高度 20px
        // 从右向左找
        for (int i = MIN_X; i < width; i++) {
            for (int j = MAX_Y; j >= MIN_Y; j--) {
                int pixel = image.getRGB(i, j);
                int r = (pixel & 0xff0000) >> 16;
                int g = (pixel & 0xff00) >> 8;
                int b = (pixel & 0xff);
                if (ToleranceHelper.match(r, g, b, R_TARGET_SWORD, G_TARGET_SWORD, B_TARGET_SWORD, 16)) {
//                    maxX = Integer.max(maxX, i);
//                    minX = Integer.min(minX, i);
//                    if (maxY == -1){
//                        maxY = j;
//                        minY = maxY - 10;
//                    }
                    ret[0] = i;
                    ret[1] = j;
                    System.out.println(maxX + ", " + minX);
                    System.out.println("pos, x: " + ret[0] + ", y: " + ret[1]);
                    return ret;
                }
            }
        }

        ret[0] = (maxX + minX) / 2;
        ret[1] = minY ;
        System.out.println(maxX + ", " + minX);
        System.out.println("pos, x: " + ret[0] + ", y: " + ret[1]);
        return ret;
    }

    public static void main(String... strings) throws IOException {
        MyPosFinder2 t = new MyPosFinder2();
        String root = t.getClass().getResource("/").getPath();
        System.out.println("root: " + root);
        String imgsSrc = root + "img/";
        String imgsDesc = root + "img/my_pos";
        File srcDir = new File(imgsSrc);
        System.out.println(srcDir);
        long cost = 0;
        File[] listFile = srcDir.listFiles();
        if (listFile == null || listFile.length == 0) return;
        for (File file : listFile) {
            if (!file.getName().endsWith(".png")) {
                continue;
            }
            System.out.println(file);
            BufferedImage img = ImgLoader.load(file.getAbsolutePath());
            long t1 = System.nanoTime();
            int[] pos = t.find(img);
            long t2 = System.nanoTime();
            cost += (t2 - t1);
            BufferedImage desc = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics graphics = desc.getGraphics();
            graphics.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null); // 绘制缩小后的图
            graphics.setColor(Color.RED);
            graphics.fillRect(pos[0] - 5, pos[1] - 5, 10, 10);
            File descFile = new File(imgsDesc, file.getName());
            if (!descFile.exists()) {
                descFile.mkdirs();
                descFile.createNewFile();
            }
            ImageIO.write(desc, "png", descFile);
        }
        System.out.println("avg time cost: " + (cost / listFile.length / 1_000_000));

    }
}
